package com.platform.service.impl;

import com.platform.dao.AuctionDao;
import com.platform.dao.AuctionRecordDao;
import com.platform.entity.AuctionEntity;
import com.platform.entity.AuctionFinishEntity;
import com.platform.entity.AuctionRecordEntity;
import com.platform.entity.UserEntity;
import com.platform.service.AuctionFinishService;
import com.platform.service.AuctionService;
import com.platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 产品竞拍价Service实现类
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-07-05 13:51:24
 */
@Service("auctionService")
public class AuctionServiceImpl implements AuctionService {
    @Autowired
    private AuctionDao auctionDao;
    @Autowired
    private AuctionRecordDao auctionRecordDao;
    @Autowired
    private AuctionFinishService auctionFinishService;
    @Autowired
    private UserService userService;

    @Override
    public AuctionEntity queryObject(Integer id) {
        return auctionDao.queryObject(id);
    }

    @Override
    public List<AuctionEntity> queryList(Map<String, Object> map) {
        return auctionDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return auctionDao.queryTotal(map);
    }

    @Override
    public int save(AuctionEntity auction) {
        return auctionDao.save(auction);
    }

    @Override
    public int update(AuctionEntity auction) {
        return auctionDao.update(auction);
    }

    @Override
    public int delete(Integer id) {
        return auctionDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return auctionDao.deleteBatch(ids);
    }

    @Transactional
    @Override
    public boolean open(Integer aid) {


        AuctionEntity au = auctionDao.queryObject(aid);
        if (au.getStatu() == 0) {
            return false;
        }
        if(au.getEndTime().getTime()>System.currentTimeMillis()){
            return false;
        }
        Integer maximum = auctionDao.selectProductLatest(aid);
        AuctionRecordEntity record = null;
        List<AuctionRecordEntity> list = auctionRecordDao.selectHighest(aid, maximum);
        if (list.size() > 1) {
            List<Long> uids = list.stream().map(AuctionRecordEntity::getUid).collect(Collectors.toList());
            List<UserEntity> users = userService.selectUserListForId(uids);
            UserEntity user = extractUser(users);
            record = list.stream().filter(o -> o.getUid() == user.getId()).findFirst().orElse(null);
        }
        Date currency = new Date(System.currentTimeMillis() + 300000);
        AuctionFinishEntity finish = AuctionFinishEntity.builder().uid(record.getUid()).phone(record.getPhone()).pid(record.getPid()).pname(record.getPname())
                .aid(record.getAid()).bid(record.getBid()).deposit(au.getDeposit()).endTime(currency).status(0).build();
        auctionFinishService.save(finish);
        au.setStatu(0);
        auctionDao.update(au);
        return true;
    }

    public UserEntity extractUser(List<UserEntity> users) {
        List<UserEntity> list = users.stream().filter(o -> o.getDanger() == 0).collect(Collectors.toList());
        int size = list.size();
        if (size == 0) {
            list = users.stream().filter(o -> o.getDanger() == 1).collect(Collectors.toList());
        }
        size = list.size();
        if (size == 0) {
            list = users;
        }
        if (size == 1) {
            return list.get(0);
        }
        Random random = new Random();
        int index = random.nextInt(size);
        return list.get(index);
    }
}
